﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Event_SG_EWDT
{
    public partial class Groups : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EventClick(liAll, null); //fake a click

                Resetddl();
            }
            foreach (Control c in ulList.Controls)
            {
                if (c is System.Web.UI.HtmlControls.HtmlAnchor)
                {
                    ((System.Web.UI.HtmlControls.HtmlAnchor)c).ServerClick += new EventHandler(EventClick);
                }
            }
        }
        
        void Resetddl()
        {
            ddl_GroupPageNumber.Items.Clear();
            int pagenumber = (int)Math.Ceiling(Convert.ToDouble(((ArrayList)Session["DisplayGroup"]).Count) / 6); // generate the Value in DDL
            for (int i = 0; i < pagenumber; i++)
            {
                ddl_GroupPageNumber.Items.Add((i + 1).ToString());
            }
        }
        
        public void EventClick(object sender, EventArgs e)
        {

            switch (((System.Web.UI.HtmlControls.HtmlAnchor)sender).ID)
            {

                case "liAll":
                    divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.GetTopGroup();
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"],0);
                    Resetddl();
                    break;
                case "liArtists":
                    divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(1);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liBusiness": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(2);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liCharity": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(3);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liCommunity": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(4);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liEducationalInsititutions": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(5);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liGaming": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(6);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liGovernment":
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(7);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liHospitals": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(8);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liNonGovernmental": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(9);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liSports":
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(10);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
                case "liOthers": 
                                        divGroups.Controls.Clear();
                    Session["DisplayGroup"] = DBQUERIES.SearchGroupViaCategory(11);
                    ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
                    Resetddl();
                    break;
            }
        }

        Control CreateCustomControl(string groupId, string groupTitle, string desc)
        {
            Control c = LoadControl("~/CustomUserControl.ascx");

            HyperLink hyperlinkEvent = (HyperLink)c.FindControl("hyperlinkEvent");
            TextBox tbxUserControl = (TextBox)c.FindControl("tbxUserControl");
            Image imgUserControl = (Image)c.FindControl("imgUserControl");

            hyperlinkEvent.Text = groupTitle;
            hyperlinkEvent.NavigateUrl += String.Format("~/ViewGroup.aspx?groupid={0}", groupId);
         

            tbxUserControl.Text = desc;

            //************************************************ NEED TO UPDATE
            //imgUserControl.ImageUrl = "~/images/groups/" + groupid + "/1.jpg";
            string id = DBMANAGER.GetImageIdGroupThumbnail(groupId);
            imgUserControl.ImageUrl = "~/HTTPHandler.ashx?imageid=" + Convert.ToInt32(id);
  

            return c;
        }
       
        void ShowInEventDiv(ArrayList a)
        {
            foreach (object o in a)
            {
                string[] s = (string[])o;

                Control c = CreateCustomControl(s[0], s[1], s[2]);

                divGroups.Controls.Add(c);
            }

        }
        
        void ShowInEventDiv(ArrayList a, int x)
        {
            if (a.Count >= 6 + (6 * x)) // if the number of events are greater than the option.
            {
                for (int i = 6 * x; i < 6 + (6 * x); i++)
                {
                    string[] s = (string[])a[i];

                    Control c = CreateCustomControl(s[0], s[1], s[2]);

                    divGroups.Controls.Add(c);
                }
            }
            else
            {
                if (a.Count != 0)
                {
                    int left = (a.Count + 6) % (6 + (6 * x));
                    for (int i = x * 6; i < left + (6 * x); i++)
                    {
                        string[] s = (string[])a[i];

                        Control c = CreateCustomControl(s[0], s[1], s[2]);

                        divGroups.Controls.Add(c);
                    }
                }
            }
        }

        protected void tbxSearchGroup_TextChanged(object sender, EventArgs e)
        {
            //Session["DisplayGroup"] = DBQUERIES.SearchGroup(tbxSearchGroup.Text);
            //divGroups.Controls.Clear();
            //ShowInEventDiv((ArrayList) Session["DisplayGroup"],0);
            //Resetddl();
        }

        protected void ddl_GroupPageNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(ddl_GroupPageNumber.SelectedValue) - 1;
            divGroups.Controls.Clear();
            ShowInEventDiv((ArrayList)Session["DisplayGroup"], page); 
        }

        protected void btnSearchGroup_Click(object sender, EventArgs e)
        {
            Session["DisplayGroup"] = DBQUERIES.SearchGroup(tbxSearchEvent.Text);
            divGroups.Controls.Clear();
            ShowInEventDiv((ArrayList)Session["DisplayGroup"], 0);
            Resetddl();
        }

    }
}
